package com.sl.au.controller;

import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.ModelAndView;

import com.sl.au.entity.Grading;
import com.sl.au.entity.Template;
import com.sl.au.entity.TemplateDetails;
import com.sl.au.entity.User;
import com.sl.au.service.GradingService;
import com.sl.au.service.TemplateDetailsService;
import com.sl.au.service.TemplateService;
import com.sl.au.service.UserService;
import com.sl.utils.MsgHelper;

@RestController
@RequestMapping(value = "/ui/template") 
public class TemplateController extends BaseController {

	@Autowired
	TemplateService templateService;
	@Autowired
	GradingService gradingService;
	@Autowired
	TemplateDetailsService templateDetailsService;
	@Autowired
	UserService userService;
	
	
	@RequestMapping(method = RequestMethod.GET)
	public ModelAndView index() {
		ModelAndView modelAndView = this.createLayoutView("template/templateIndex");
		List<Grading> gradings = gradingService.getRepository().findGradingAll();
		modelAndView.addObject("gradings", gradings);
		return modelAndView;
	}
	
	/**
	 * 模板页面数据
	 * @param request
	 * @return
	 */
	@RequestMapping(value = "getpage", method = RequestMethod.POST)
	public Map<String,Object> getPage(HttpServletRequest request) {
		Map<String,Object> map = new HashMap<String,Object>();
		int start = Integer.parseInt(request.getParameter("start"));
		int length = Integer.parseInt(request.getParameter("length"));
		String key=request.getParameter("key");
		List<Template> modules=null;
		long count = 0;
		
		modules = templateService.getPageByKey(start, length, key, null, null);
		count = templateService.getPageByKeyCount(key);
		
		map.put("data", modules);
		map.put("draw", request.getParameter("draw"));
		map.put("recordsFiltered", count);
		map.put("recordsTotal", count);
		return map;
	}
	
	/**
	 * 模板页面数据
	 * @param request
	 * @return
	 */
	@RequestMapping(value = "getNotSetUser", method = RequestMethod.POST)
	public Map<String,Object> getNotSetUserPage(HttpServletRequest request) {
		Map<String,Object> map = new HashMap<String,Object>();
		int start = Integer.parseInt(request.getParameter("start"));
		int length = Integer.parseInt(request.getParameter("length"));
		String key=request.getParameter("key");
		String templateId=request.getParameter("templateId");
		List<User> modules = new ArrayList<User>();
		int count = 0;
		if(StringUtils.hasText(templateId)) {
			modules = templateService.getNotSetUserPageByKey(start, length, templateId, key, null, null);
			count = templateService.getNotSetUserPageByKeyCount(templateId, key);
		}
		map.put("data", modules);
		map.put("draw", request.getParameter("draw"));
		map.put("recordsFiltered", count);
		map.put("recordsTotal", count);
		return map;
	}

	/**
	 * 模板页面数据
	 * @param request
	 * @return
	 */
	@RequestMapping(value = "getSetUser", method = RequestMethod.POST)
	public Map<String,Object> getSetUserPage(HttpServletRequest request) {
		Map<String,Object> map = new HashMap<String,Object>();
		int start = Integer.parseInt(request.getParameter("start"));
		int length = Integer.parseInt(request.getParameter("length"));
		String key=request.getParameter("key");
		String templateId=request.getParameter("templateId");
		String gradingId=request.getParameter("gradingId");
		List<User> modules= new ArrayList<User>();
		int count = 0;
		if(StringUtils.hasText(gradingId) && StringUtils.hasText(templateId)) {
			modules = templateService.getSetUserPageByKey(start, length, templateId, gradingId, key, null, null);
			count = templateService.getSetUserPageByKeyCount(templateId, gradingId, key);
		}
		map.put("data", modules);
		map.put("draw", request.getParameter("draw"));
		map.put("recordsFiltered", count);
		map.put("recordsTotal", count);
		return map;
	}
	
	@RequestMapping(value = "getGrading/{templateId}", method = RequestMethod.POST)
	public Map<String,Object> getGrading(@PathVariable("templateId") String templateId) {
		Map<String,Object> map = new HashMap<String,Object>();
		List<Grading> gradings = templateDetailsService.getRepository().getGradingByTemplate(templateId);
		map.put("gradings", gradings);
		return map;
	}
	
	/**
	 * 保存
	 * @return
	 */
	@RequestMapping(value = "/save", method = RequestMethod.POST)
	public Map<String, Object> saveTemplate(HttpServletRequest request, @RequestBody Template template) {
		Map<String, Object> map = new HashMap<String, Object>();
		templateService.saveTemplate(template);
		map.put("code", 1);
		map.put("msg", "模板信息保存成功");
		return map;
	}
	
	/**
	 * 删除信息
	 * @param id
	 * @return
	 */
	@RequestMapping("/delete/{id}")
	public MsgHelper deleteTemplate(@PathVariable("id") String id) {
		MsgHelper msgHelper = MsgHelper.success();
		templateDetailsService.getRepository().deleteAllTemplateDetailsOfTemplate(id);
		templateService.getRepository().delete(id);
		return msgHelper;
	}
	
	@RequestMapping("/addGrading/{templateId}/{gradingId}")
	public MsgHelper addGrading(@PathVariable("templateId") String templateId, @PathVariable("gradingId") String gradingId) {
		MsgHelper msgHelper = MsgHelper.success();
		Template template = templateService.getRepository().findOne(templateId);
		Grading grading = gradingService.getRepository().findOne(gradingId);
		TemplateDetails templateDetails = new TemplateDetails();
		templateDetails.setTemplate(template);
		templateDetails.setGrading(grading);
		templateDetailsService.getRepository().save(templateDetails);
		return msgHelper;
	}

	@RequestMapping("/deleteGrading/{templateId}/{gradingId}")
	public MsgHelper deleteGrading(@PathVariable("templateId") String templateId, @PathVariable("gradingId") String gradingId) {
		MsgHelper msgHelper = MsgHelper.success();
		Template template = templateService.getRepository().findOne(templateId);
		Grading grading = gradingService.getRepository().findOne(gradingId);
		templateDetailsService.deleteTemplateDetails(template, grading);
		return msgHelper;
	}

	@RequestMapping("/addUser/{templateId}/{gradingId}/{userId}")
	public MsgHelper addUser(@PathVariable("templateId") String templateId, @PathVariable("gradingId") String gradingId,  @PathVariable("userId") String userId) {
		MsgHelper msgHelper = MsgHelper.success();
		/*
		//暂时不设置数据问题
		TemplateDetails templateDetails = templateDetailsService.getRepository().getTemplateOfGradingAndNotUser(templateId, gradingId);
		User user = userService.getRepository().findOne(userId);
		if(templateDetails == null){
			templateDetails = new TemplateDetails();
			Template template = templateService.getRepository().findOne(templateId);
			Grading grading = gradingService.getRepository().findOne(gradingId);
			templateDetails.setGrading(grading);
			templateDetails.setTemplate(template);
		}
		templateDetails.setUser(user);
		templateDetailsService.getRepository().save(templateDetails);
		*/
		
		TemplateDetails templateDetails = new TemplateDetails();
		Template template = templateService.getRepository().findOne(templateId);
		Grading grading = gradingService.getRepository().findOne(gradingId);
		User user = userService.getRepository().findOne(userId);
		templateDetails.setUser(user);
		templateDetails.setGrading(grading);
		templateDetails.setTemplate(template);
		templateDetailsService.getRepository().save(templateDetails);
		msgHelper.setMsg("人员设置成功");
		return msgHelper;
	}

	@RequestMapping("/removeUser/{templateId}/{gradingId}/{userId}")
	public MsgHelper removeUser(@PathVariable("templateId") String templateId, @PathVariable("gradingId") String gradingId,  @PathVariable("userId") String userId) {
		MsgHelper msgHelper = MsgHelper.success();
		/*
		//暂时不设置数据问题
		List<TemplateDetails> list = templateDetailsService.getRepository().getTemplateOfGrading(templateId, gradingId);
		if(list.size() > 1) {
			templateDetailsService.getRepository().deleteTemplateDetailsOfUser(templateId, gradingId, userId);
		}else {
			TemplateDetails templateDetails = list.get(0);
			templateDetails.setUser(null);
			templateDetailsService.getRepository().save(templateDetails);
		}
		*/
		templateDetailsService.getRepository().deleteTemplateDetailsOfUser(templateId, gradingId, userId);
		msgHelper.setMsg("人员移除成功");
		return msgHelper;
	}
	
	@RequestMapping(value="/addUserAll/{templateId}/{gradingId}",method = RequestMethod.POST)
	public Map<String,Object> addUserAll(@PathVariable("templateId") String templateId, @PathVariable("gradingId") String gradingId, @RequestBody Map<String,Object> map){
		Map<String,Object> msg = new HashMap<String,Object>();
		ArrayList<String> dataInfo = (ArrayList<String>) map.get("dataInfo");
		Template template = templateService.getRepository().findOne(templateId);
		Grading grading = gradingService.getRepository().findOne(gradingId);
		for(String userId : dataInfo) {
			TemplateDetails templateDetails = new TemplateDetails();
			User user = userService.getRepository().findOne(userId);
			templateDetails.setUser(user);
			templateDetails.setGrading(grading);
			templateDetails.setTemplate(template);
			
			templateDetailsService.getRepository().save(templateDetails);
		}
		msg.put("msg", "人员设置成功!");
		return msg;
	};
	
	@RequestMapping(value="/removeUserAll/{templateId}/{gradingId}",method = RequestMethod.POST)
	public Map<String,Object> removeUserAll(@PathVariable("templateId") String templateId, @PathVariable("gradingId") String gradingId, @RequestBody Map<String,Object> map){
		Map<String,Object> msg = new HashMap<String,Object>();
		ArrayList<String> dataInfo = (ArrayList<String>) map.get("dataInfo");
		String[] users = new String[dataInfo.size()];
		dataInfo.toArray(users);
		templateDetailsService.getRepository().deleteAllTemplateDetailsOfUser(templateId, gradingId, users);
		msg.put("msg", "人员移除成功!");
		return msg;
	};
}
